@extends('Admin.base')
@section('css')
    <link rel="stylesheet" href="/Admin/layuiadmin/css/extend/formSelects-v4.css" media="all">
    <style type="text/css">
        #pull_right{
            text-align:center;
        }
        .pull-right {
            /*float: left!important;*/
        }
        .pagination {
            display: inline-block;
            padding-left: 0;
            margin: 20px 0;
            border-radius: 4px;
        }
        .pagination > li {
            display: inline;
        }
        .pagination > li > a,
        .pagination > li > span {
            position: relative;
            float: left;
            padding: 6px 12px;
            margin-left: -1px;
            line-height: 1.42857143;
            color: #3e9688;
            text-decoration: none;
            background-color: #fff;
            border: 1px solid #ddd;
        }
        .pagination > li:first-child > a,
        .pagination > li:first-child > span {
            margin-left: 0;
            border-top-left-radius: 4px;
            border-bottom-left-radius: 4px;
        }
        .pagination > li:last-child > a,
        .pagination > li:last-child > span {
            border-top-right-radius: 4px;
            border-bottom-right-radius: 4px;
        }
        .pagination > li > a:hover,
        .pagination > li > span:hover,
        .pagination > li > a:focus,
        .pagination > li > span:focus {
            color: #2a6496;
            background-color: #eee;
            border-color: #ddd;
        }
        .pagination > .active > a,
        .pagination > .active > span,
        .pagination > .active > a:hover,
        .pagination > .active > span:hover,
        .pagination > .active > a:focus,
        .pagination > .active > span:focus {
            z-index: 2;
            color: #fff;
            cursor: default;
            background-color: #3e9688;
            border-color: #3e9688;
        }
        .pagination > .disabled > span,
        .pagination > .disabled > span:hover,
        .pagination > .disabled > span:focus,
        .pagination > .disabled > a,
        .pagination > .disabled > a:hover,
        .pagination > .disabled > a:focus {
            color: #777;
            cursor: not-allowed;
            background-color: #fff;
            border-color: #ddd;
        }
        .clear{
            clear: both;
        }
    </style>
@endsection
@section('content')

    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>
                    @if($id == 0)
                        全部订单
                    @elseif($id == 1)
                        待付款
                    @elseif($id == 2)
                        待发货
                    @elseif($id == 3)
                       待收货
                    @elseif($id == 4)
                        待取货
                    @elseif($id == 5)
                        已完成
                    @elseif($id == 6)
                        申请售后中
                    @endif
                </legend>
            </fieldset>
        </div>
        <div class="layui-form layui-card-header layuiadmin-card-header-auto layui-bg-gray">
            <div class="layui-form-item layui-row">


                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                今日成交<br>
                                ¥ {{$saleMoney['Today']}}
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                过去7天成交<br>
                                ¥ {{$saleMoney['LastWeek']}}
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                本月成交<br>
                                ¥{{$saleMoney['Month']}}
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                上月成交<br>
                                ¥ {{$saleMoney['LastMonth']}}
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                今日完成订单<br>
                                {{$order['Today'] }}
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                过去7天订单<br>
                                {{$order['LastWeek'] }}
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                本月订单<br>
                                {{$order['Month'] }}
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                上月订单<br>
                                {{$order['LastMonth'] }}
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                今日销(盒)<br>
                                {{$sale['Today']}}
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                过去7天销量<br>
                                {{$sale['LastWeek']}}
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                本月销量<br>
                                {{$sale['Month']}}
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md2">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                上月销量<br>
                                {{$sale['LastMonth']}}
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form layui-card-header layuiadmin-card-header-auto ">
                    <form class="layui-form" action="" method="get">
                        <div class="layui-form-item layui-row">
                            <div class="layui-inline layui-col-md5">
                                <label class="layui-form-label">关键词</label>
                                <div class="layui-input-block">
                                    <input type="text" name="keyword"
                                           @if($payload['keyword'])
                                                   value="{{$payload['keyword']}}"
                                                   @endif
                                           placeholder="订单号、收货人姓名、手机号码" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <div class="layui-btn-group layui-input-block">
                                    <button class="layui-btn layui-btn-sm layui-input-block layuiadmin-btn-admin" lay-submit lay-filter="LAY-search">
                                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                    </button>
                                    <a class="layui-btn layui-btn-sm layui-input-block layuiadmin-btn-admin" href="?keyword=&time=&type=&status=&pay_type=&user_type=" id="LAY-clean">
                                        清空
                                    </a>
                                </div>
                            </div>

                        </div>
                    <div class="layui-form-item layui-row">
                        <div class="layui-inline">
                            <label class="layui-form-label">下单日期:</label>
                            <div class="layui-input-block">
                                <input type="text" name="time"  autocomplete="off" class="layui-input" id="time-range" >
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">售卖类型</label>
                            <div class="layui-input-block">
                            <select name="type">
                                <option value=''>全部售卖类型</option>
                                @foreach (\App\Models\Order::TYPE as $status => $name)
                                        <option value="{{$status ?? 0}}"  {{$payload['type']==$status? "selected" : ""}}>{{$name ?? ''}}</option>
                                @endforeach
                            </select>
                            </div>
                        </div>
                        @if($id == 0 )
                        <div class="layui-inline">
                            <label class="layui-form-label">订单状态</label>
                            <div class="layui-input-block">
                                <select name="status">
                                    <option value=''>全部状态</option>
                                    @foreach (\App\Models\Order::STSTUS as $status => $name)
                                        @if($status !=\App\Models\Order::STATUS_CANCEL)
                                            <option value="{{$status ?? 0}}"  {{$payload['status']==$status? "selected" : ""}} >{{$name ?? ''}}</option>
                                        @endif
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        @endif
                        <div class="layui-inline">
                            <label class="layui-form-label">支付方式</label>
                            <div class="layui-input-block">
                                <select name="pay_type">
                                    <option value=''>全部支付方式</option>
                                    @foreach (\App\Models\Order::PAY_TYPE as $status => $name)
                                        <option value="{{$status ?? 0}}"  {{$payload['pay_type']==$status? "selected" : ""}}>{{$name ?? ''}}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">运营人员</label>
                            <div class="layui-input-block">
                                <select name="user_type">
                                    <option value=''>不限</option>
                                    <option value='{{\App\Models\User::STAFF}}' {{$payload['user_type']==\App\Models\User::STAFF ? "selected" : ""}}>员工推广</option>
                                    <option value='{{\App\Models\User::USER}}' {{$payload['user_type']==\App\Models\User::USER ? "selected" : ""}}>非员工推广</option>
                                </select>
                            </div>
                        </div>
                        <input type="hidden" name="id" value="{{$id}}">
                    </div>
                    </form>
                </div>
        <div class="layui-card-body">
            {{--<div class="layui-table-tool">--}}
                {{--<div class="layui-table-tool-temp">--}}
                    {{--<div class="layui-btn-container">--}}
                         <button class="layui-btn export" lay-event="export">导 出</button>
                    {{--</div>--}}
                {{--</div>--}}
            {{--</div>--}}
            <table class="layui-table"  lay-filter="LAY-table" id="table" >
                <thead>
                    <tr>
                       <th><input type="checkbox" lay-skin="primary" class="checkboxAll"  lay-filter="allChoose"></th>
                       <th>商品</th>
                       <th>买家信息</th>
                       <th>支付信息</th>
                       <th>配送信息</th>
                       <th>所属员工信息</th>
                       <th>状态</th>
                        @if($id == \App\Models\Order::STATUS_WAIT_DELIVER or $id == \App\Models\Order::STATUS_AFTER_ING)
                            <th>操作</th>
                        @endif
                    </tr>
                </thead>
                <tbody>
                   @foreach($tabledata as $key=>$value)
                       <tr>
                           <td align="center" colspan="8" >
                               下单时间: {{$value->created_at}}&nbsp;&nbsp;
                               @if(isset($value->order_type) and $value->order_type ==1)
                                <span class="layui-btn layui-btn-normal layui-btn-sm">{{\App\Models\Order::TYPE[$value->order_type]}}</span>
                               @elseif(isset($value->order_type) and $value->order_type ==2)
                                   <span class="layui-btn layui-btn-warm layui-btn-sm">{{\App\Models\Order::TYPE[$value->order_type]}}</span>
                               @elseif(isset($value->order_type) and $value->order_type ==3)
                                   <span class="layui-btn layui-btn-sm">{{\App\Models\Order::TYPE[$value->order_type]}}</span>
                               @endif&nbsp;&nbsp;
                               订单号:{{$value->online_order}}
                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                               <a  href="/admin/order/{{$value->id}}/detail">查看订单详情</a>
                           </td>
                       </tr>
                       <tr>
                           <td width="5px"><input type="checkbox" name="check[]" value="{{$value->id}}" lay-skin="primary" lay-filter="oneChoose"></td>
                           <td>
                               @if(isset($value->OrderGoods))
                               @foreach($value->OrderGoods as $k=>$v)
                                       <img src="{{$v->photo}}" alt="" width="30px" height="30px">{{$v->title}} ✖️ {{$v->amount}} <br>
                               @endforeach
                               @endif
                           </td>
                           <td>
                               @if($value->order_type == \App\Models\Order::TYPE_ONLINE)
                                   {{$value->user_name}}<br>
                                   ID:{{$value->user_id}}<br>
                                   {{$value->user_phone}}<br>
                                   {{$value->user_region}}<br>
                                   {{$value->user_address}}<br>
                               @else
                                   {{$value->userInfo ? $value->userInfo->nickname :""}}<br>
                                   ID:{{$value->user_id}}<br>
                                   {{$value->userInfo ? $value->userInfo->realname :""}}<br>
                                   {{$value->userInfo ? $value->userInfo->phone :""}}<br>
                                   {{$value->get_region}}<br>
                               @endif

                           </td>
                           <td>¥{{round($value->real_pay,2)}} <br> {{array_key_exists($value->pay_type,\App\Models\Order::PAY_TYPE)  ? \App\Models\Order::PAY_TYPE[$value->pay_type] :''}} </td>
                           <td>
                               @if($value->order_type == \App\Models\Order::TYPE_ONLINE )
                                    {{array_key_exists($value->express_name, \App\Models\Order::EXPRESS)? \App\Models\Order::EXPRESS[$value->express_name]:''}} <br> {{$value->express_order}}
                               @else
                                   不需要配送
                               @endif
                           </td>
                           <td>
                               {{$value->inviteUserInfo_realname}}<br>
                               {{$value->inviteUserInfo_id}}<br>
                               {{$value->inviteUserInfo_department}}<br>
                           </td>
                           <td>
                               {{$value->status== \App\Models\Order::STATUS_CANCEL ? "已超时" : (array_key_exists($value->status,\App\Models\Order::STSTUS) ? \App\Models\Order::STSTUS[$value->status] : '')}}
                           </td>
                           @if($id == \App\Models\Order::STATUS_WAIT_DELIVER)
                              <td> <span class="fahuo layui-btn layui-btn-green layui-btn-xs" id="{{$value->id}}" user_name="{{$value->user_name}}" user_phone="{{$value->user_phone}}"user_region="{{$value->user_region}}"user_address="{{$value->user_address}}" > 确认发货</span></td>
                           @elseif($id==\App\Models\Order::STATUS_AFTER_ING)
                               <td ><span class="tuikuan layui-btn layui-btn-green layui-btn-xs"  data="{{$value->id}}" pay_type="{{$value->pay_type ? (array_key_exists($value->pay_type,\App\Models\Order::PAY_TYPE) ? \App\Models\Order::PAY_TYPE[$value->pay_type] :'') :""}}" user_name="{{$value->user_name}}">发起退款</span> <span class="bohui layui-btn layui-btn-green layui-btn-xs"  data="{{$value->id}}"> 驳回</span></td>
                           @endif
                       </tr>
                   @endforeach
                </tbody>
            </table>
        {!! $table->render() !!} <span style="font-weight: 400">共 {{$total}} 条数据 总金额 {{$totalmoney}} 元</span>
        </div>

    </div>
@endsection
@section('script')
    <script src="/js/ajax.js"></script>
    <script>
        layui.use(['form', 'table','laydate'], function(){
            var $ = layui.$
                    ,form = layui.form
                    ,table = layui.table
                    ,laydate = layui.laydate;
            laydate.render({
                elem: '#time-range'
                ,type: 'date'
                ,range: true
            });
            form.on('checkbox(allChoose)', function (data) {
                $("input[name='check[]']").each(function () {
                    this.checked = data.elem.checked;
                });
                form.render('checkbox');
            });

            $(".fahuo").click(function (obj) {
                var id = $(this).attr('id');
                var user_address = $(this).attr('user_address');
                var user_name = $(this).attr('user_name');
                var user_region = $(this).attr('user_region');
                var user_phone = $(this).attr('user_phone');
                layer.open({
                    content:
                   ' <div class="layui-card">'+
                   '     <div class="layui-card-body">'+
                   '         <form class="layui-form" id="layui-form" >'+
                   '             <div class="layui-form-item">'+
                   '                 <label class="layui-form-label">收货人</label>'+
                   '                 <div class="layui-input-block" >'+
                                      user_name+
                   '                 </div>'+
                   '             </div>'+
                   '             <div class="layui-form-item">'+
                   '                 <label class="layui-form-label">联系电话</label>'+
                   '                 <div class="layui-input-block" >'+
                                    user_phone+
                   '                 </div>'+
                   '             </div>'+
                   '             <div class="layui-form-item">'+
                   '                 <label class="layui-form-label">收货地区</label>'+
                   '                 <div class="layui-input-block" >'+
                                    user_region+
                   '                 </div>'+
                   '             </div>'+
                   '             <div class="layui-form-item">'+
                   '                 <label class="layui-form-label">详细地址</label>'+
                   '                 <div class="layui-input-inline">'+
                                    user_address+
                   '                 </div>'+
                   '             </div>'+
                   '             <div class="layui-form-item">'+
                   '                 <label class="layui-form-label">快递公司</label>'+
                   '                 <div class="layui-input-inline">'+
                   '                     <select id="express_name" lay-verify="required">'+
                   '                   @foreach(\App\Models\Order::EXPRESS as $key=>$value)'+
                   '                         <option value="{{$key}}" @if($key=="yunda") selected @endif>{{$value}}</option>'+
                   '                        @endforeach'+
                   '                     </select>'+
                   '                 </div>'+
                   '             </div>'+
                   '             <div class="layui-form-item">'+
                   '                 <label class="layui-form-label">快递单号</label>'+
                   '                 <div class="layui-input-inline">'+
                   '                     <input type="text" id = "express_order" class="layui-input" lay-verify="required">'+
                   '                 </div>'+
                   '             </div>'+
                   '             <input type="hidden" name="id" id="fahuoid" value="'+ id +'">'+
                   '                 <div class="layui-form-item">'+
                   '                    {{csrf_field()}}'+
                   '                     <div class="layui-input-block">'+
                   '                         <button type="reset" class="layui-btn layui-btn-primary" id="fahuoqx-btn">取消</button>'+
                   '                         <button type="submit" class="layui-btn" id="fahuo-btn">确认发货</button>'+
                   '                     </div>'+
                   '                 </div>'+
                   '         </form>'+
                   '     </div>'+
                   ' </div>'
                    ,type:1
                    ,area: ['500px', '520px']
                });
                $("#fahuoqx-btn").click();
            });

            $(".tuikuan").click(function (obj) {
                var id = $(this).attr('data');
                var user_name = $(this).attr('user_name');
                var pay_type = $(this).attr('pay_type');
                layer.open({
                    title: '退款'
                    ,content:'<div class="layui-card">\n' +
                '    <div class="layui-card-body">\n' +
                '        <div class="layui-form" lay-filter="">\n' +
                '            <div class="layui-form-item">\n' +
                '                <label class="layui-form-label">发起退款人</label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <span class="layui-form-mid">'+ user_name +'</span>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '            <div class="layui-form-item">\n' +
                '                <label class="layui-form-label">退回路径</label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <span class="layui-form-mid">'+ pay_type +'</span>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '    <input type="hidden" id="refund"  value="'+id+'"  class="layui-input">\n' +
                '            <div class="layui-form-item">\n' +
                '                <label class="layui-form-label"></label>\n' +
                '                    <button type="reset" class="layui-btn" id="tuikuanquxiao-btn">取消</button>\n' +
                '                    <button type="submit" class="layui-btn" value="" id="tuikuan-btn">确认退款</button>\n' +
                '            </div>\n' +
                '        </div>\n' +
                '    </div>\n' +
                '</div>'
                    ,type:1
                    ,area: ['500px', '230px']
                });
            });

            $('body').on('click','#tuikuan-btn',function(){
                var order_id = $('#refund').val();
                $.ajax({
                    url: '{{route('order.refund')}}'
                    , type: 'POST'
                    , dataType: 'json'
                    , data: {id:order_id}
                    , beforeSend:function(){
                        layer.load(2);
                    }
                    , success: function (ret) {
                        if (ret.hasOwnProperty('code') && ret.code == 200) {
                            layer.msg(ret.message,{icon: 1,time:2000});
                            return false;
                        }
                        layer.msg(ret.message,{icon:2});
                    }
                    , complete: function(){
                        window.location.reload()
                    }
                });
            });
            $('body').on('click','#tuikuanquxiao-btn',function(){
                layer.closeAll();
            });
            $('body').on('click','#fahuo-btn',function(){
                var id = $("#fahuoid").val();
                var express_name = $('#express_name').val();
                var express_order = $('#express_order').val();
                if(express_order == ""){
                    layer.msg("请输入运单号");
                    return false;
                }
                if(!/^[a-z0-9A-Z]{1,20}$/.test(express_order)){
                    layer.msg("运单号格式必须为数字或字母 不能超过二十位!");
                    return false;
                }
                $.ajax({
                    url: '{{route('order.deliverSave')}}'
                    , type: 'POST'
                    , dataType: 'json'
                    , data: {id:id,express_name:express_name,express_order:express_order}
                    , beforeSend:function(){
                        layer.load(2);
                    }
                    , success: function (ret) {
                        if (ret.hasOwnProperty('code') && ret.code == 200) {
                            layer.msg(ret.message,{icon: 1,time:2000});
                        }else{
                            layer.msg(ret.message,{icon:2,time:2000});
                        }
                    }
                    , complete: function(){
                        window.location.reload()
                    }
                });


            });

            $(".export").click(function(obj){
                var items =[];
                $("input[name='check[]']:checked").each(function(){
                    items.push($(this).val());
                })
                layer.prompt({
                    formType: 0,
                    value: '',
                    title: '请输入文件名称',
                    btn:['导出','取消']
                }, function(value, index, elem){
                    var field= {};
                    if(value == '' || value == undefined){
                        layer.msg('请输入文件名称');
                    }else{
                        field.filename = value;
                        field.ids = items;
                        field.id = '{{$id}}';
                        field.keyword = $("input").val();
                        field.type = $("select[name='type']").val();
                        field.time = $("select[name='time']").val();
                        field.status = $("select[name='status']").val();
                        field.pay_type = $("select[name='pay_type']").val();
                        field.user_type = $("select[name='user_type']").val();
                        $.ajax({
                            url: '/admin/order/export'
                            , type: 'POST'
                            , dataType: 'json'
                            , data: field
                            , success: function (ret) {
                                if (ret.hasOwnProperty('code') && ret.code == 200) {
                                    layer.msg(ret.message,{icon: 1,time:2000});
                                    return false;
                                }
                                layer.msg(ret.message,{icon:2});
                            }
                        });
                        layer.close(index);
                    }

                });
            });

            $(".bohui").click(function (obj) {
                var id = $(this).attr('data');
                layer.confirm('确认驳回吗?', function(index){
                    $.ajax({
                        url: '{{route('order.reject')}}'
                        , type: 'POST'
                        , dataType: 'json'
                        , data: {id:id}
                        , success: function (ret) {
                            if (ret.hasOwnProperty('code') && ret.code == 200) {
                                layer.msg(ret.message,{icon: 1});
                                return false;
                            }
                            layer.msg(ret.message,{icon:2});
                        }

                    });
                    window.location.reload()
                });

            });

            //监听工具条
            table.on('tool(LAY-table)', function(obj){
                var data = obj.data;
                if(obj.event === 'export'){

                }
            });
            //监听搜索
            form.on('submit(LAY-search)', function(data){
                var field = data.field;
                table.reload('LAY-table', {
                    where: field,page: {curr: 1}
                });
            });

        });



    </script>

@endsection